// 导入api接口
import { carcheckAdd, carcheckDelete, carCheckDownExl, carcheckList, carcheckUpdate, checkCarImgList, cpList, delImgCarCheck, sjList, systemCheckOrgList, systemCheckTypeList } from '@/api/api'
import { dateRevert, imgFilter } from '@/utils/businessTools'
import configJs from '@/utils/configJs'
import { FloatFun } from '@/utils/tools'
import defaultData from './data'
var data = JSON.parse(JSON.stringify(defaultData))
var adapter = {
	data,
	methods: {
		// ...mapActions(['logOut']),
		/**
		 * 初始化
		 */
		initPage(){
			//还原页面对象
			configJs.revert(data, defaultData);
			this.recordSearchObj[0].cph = this.$store.state.app.navcp; // 写入公共车牌号
		},
		
		/**
		 * 查询员工列表
		 */
		async search(val) {
			if (!this.$store.getters.pageAuth.authoritySearch) {
				return
			}
			this.revertActive() // 重置选择行
			let params = {...this.recordSearchObj[0]}
			// 处理时间
			params.jcyxjzrq = dateRevert(params.jcyxjzrq)
			params.jcpdrq = dateRevert(params.jcpdrq)
			typeof val != 'number' && (val = 1)
			params.page = val
			const res = await carcheckList(params)
			if (res.code === 0) {
			  this.page = res.data
			} else {
				this.$message(res.msg)
			}
		},

		/**
		 * 刷新
		 */
		async refresh() {
			this.recordSearchObj = [{
				cph: '',
				jcpdlb: '',
				jcpddw: '',
				jcpdrq: ['',''],
				jcyxjzrq: ['',''],
				bgbh: '',
				djry: '',
				bz: '',
			}]
			this.search()
		},

		/**
		 * 增加
		 */
		add() {
			this.resetAdd()
			this.addVisi = true
		},

		/**
		 * 重置增加弹窗
		 */
		resetAdd() {
			this.addRecord = [{
				cph: '',
				jcpdlb: '',
				jcpddw: '',
				jcpdrq: '',
				jcyxjzrq: '',
				bgbh: '',
				djry: '',
				bz: '',
			}]
		},

		/**
		 * 增加点击
		 */
		async addClick() {
			// const valid = await this.$refs['addStaff'].validate() // 校验
			// if(valid) {
				const res = await carcheckAdd(this.addRecord[0])
				if (res.code === 0) {
					this.$message('添加成功')
					this.addVisi = false
					this.search()
				} else {
					this.$message(res.msg)
				}
			// }
		},

		/**
		 * 增加、修改计算
		 */
		listComputed(record) {
			// 计算库存剩余数量
			let dqzmkcsl = record.dqzmkcsl
			let pdkcsl = record.pdkcsl
			let pdcysl = 0
			if(dqzmkcsl == '' || pdkcsl == '') {
			} else {
				pdcysl = FloatFun.sub(pdkcsl, dqzmkcsl)
			}
			record.pdcysl = pdcysl
			console.log(pdcysl)
			record.pdzt = (pdcysl > 0 ? '盘盈' : (pdcysl == 0 ? '盘平' : '盘亏'))
			// 计算出库金额
			let dj = record.dj
			let pdykje = 0
			if (dj == '' || pdcysl == '') {
			} else {
				pdykje = FloatFun.multi(dj, Math.abs(pdcysl))
			}
			record.pdykje = pdykje
		},
		addComputed() {
			this.listComputed(this.addRecord[0])
		},
		editComputed() {
			this.listComputed(this.editRecord)
		},

		 /**
			* 修改
		  */
		edit(car) {
			this.editRecord = {...car}
			this.editVisi = true
		},

		/**
		 * 日期转换
		 */
		dateFormat(date) {
			if ('' === date) {
				return date
			} else {
				if (typeof date == 'String') {
					let arr = date.split('_')
					return arr
				} else {
					if (Array.isArray(date) && date.length == 2) {
						return date[0] + '_' + date[1]
					} else {
						return date
					}
				}
			}
		},

		/**
		 * 修改点击
		 */
		async editClick() {
			const res = await carcheckUpdate(this.editRecord)
			if (res.code === 0) {
				this.$message('修改成功')
				this.editVisi = false
				this.search()
			} else {
				this.$message(res.msg)
			}
		},

		/**
		 * 删除
		 */
		async deleteClick(id) {
			await this.$confirm('确认删除?', '', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			})
			const res = await carcheckDelete(id)
			if (res.code === 0) {
				this.$message('修改成功')
				this.editVisi = false
				this.search()
			} else {
				this.$message(res.msg)
			}
		},

		/**
		 * 点击行
		 */
		rowClick(row, column, e) {
			if (row.id != this.activeRecord.id) {
				this.activeRecord = row
				this.getImgList()
			}
		},

		/**
		 * 重置选择行
		 */
		revertActive() {
			this.activeRecord = {
				id: '',
				cph: '',
				jcpdlb: '',
				jcpddw: '',
				jcpdrq: '',
				jcyxjzrq: '',
				bgbh: '',
				djry: '',
				bz: '',
			}
			this.activeImgList = []
		},

		/**
		 * 日期选择
		 */
		 datePick() {
			 console.error('datepick')
			 console.log(arguments)
			 return 1
		 },

		/**
		 * 车牌列表
		 */
		async getListCp() {
			const res = await cpList()
			if (res.code === 0) {
			  this.cpList = res.data.list
			} else {
				this.$message(res.msg)
			}
		},

		/**
		 * 司机列表
		 */
		async getListSj() {
			const res = await sjList()
			if (res.code === 0) {
			  this.sjList = res.data.list
			} else {
				this.$message(res.msg)
			}
		},

		/**
		 * 检测评定类别列表
		 */
		 async getListCheckType() {
			const res = await systemCheckTypeList()
			if (res.code === 0) {
			  this.checktypeList = res.data.list
			} else {
				this.$message(res.msg)
			}
		},

		/**
		 * 检测评定单位列表
		 */
		 async getListCheckOrg() {
			const res = await systemCheckOrgList()
			if (res.code === 0) {
			  this.checkorgList = res.data.list
			} else {
				this.$message(res.msg)
			}
		},

		/**
		 * 获取图片列表
		 */
		async getImgList() {
			const res = await checkCarImgList(this.activeRecord.id)
			if (res.code === 0) {
				this.activeImgList = imgFilter(res.data.list)
			}
		},

		/**
		 * 删除图片
		 */
		async imgDel(fileName) {
			await this.$confirm('确认删除?', '', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			})
			const res = await delImgCarCheck(fileName)
			if (res.code === 0) {
				this.$message('删除成功')
				this.getImgList()
			} else {
				this.$message(res.msg)
			}
		},

		/**
		 * 下载excel
		 */
		 async downloadExl() {
			carCheckDownExl()
		},
	},
	computed: {
	}
}
export default adapter
